
#include "setup.h"
#include <Arduino.h>
#include <Flash.h>
#include <EEPROM.h>
#include "MyEeprom.h"
#include "macros.h"


Setup::Setup()
{
  m_phoneNumberCount = -1;
  m_myEeprom = MyEeprom();
}

//procesado de comandos
boolean Setup::addNumber(const char* newNumber)
{
  boolean bRes = false;
  int count = getPhoneNumberCount();
  boolean numberExists = false;
  for (int i = 0; i < count; i++)
  {
    char currNumber [PHONE_NUMBER_BYTES + 1] = "";
    getPhoneNumberAtIndex(i, currNumber);
    if (currNumber != NULL)
    {
      if (strcmp(currNumber, newNumber) == 0)
      {
        numberExists = true;
        DEBUG_PRINT(F("Numero no grabado en EEPROM porque ya existía"));
      }
    }
  }
  if (!numberExists)
  {
    int index = count;
    //Si ya hay tantos números como es posible, sobreescribimos el último
    if (index >= MAX_PHONE_NUMBERS)
      index = MAX_PHONE_NUMBERS - 1;
    setPhoneNumber(newNumber, index);
    setPhoneNumberCount(index + 1);
    DEBUG_PRINT(F("Nuevo numero ("));
    DEBUG_PRINT(newNumber);
    DEBUG_PRINT(F(") grabado en eeprom en indice "));
    DEBUG_PRINT(index);
    DEBUG_PRINTLN();
    bRes = true;
  }
  return bRes;
}

boolean Setup::deleteNumber(const char* number)
{
  boolean deleted = false;
  int count = getPhoneNumberCount();
  boolean numberExists = false;
  for (int i = 0; i < count; i++)
  {
    char currNumber [PHONE_NUMBER_BYTES + 1] = "";
    getPhoneNumberAtIndex(i, currNumber);
    if (currNumber != NULL)
    {
      if (strcmp(currNumber, number) == 0)
      {
        numberExists = true;
        //a partir de ahora hay un número menos
        setPhoneNumberCount(count - 1);
        deleted = true;
      }
    }
    //Si se ha encontrado el número a borrar, traslado los que queden por delante 
    //a una posición menos para borrar el hueco que ha dejado
    if (numberExists)
    {
      getPhoneNumberAtIndex(i + 1, currNumber);
      setPhoneNumber((const char*)currNumber, i);
    }
  }

  return deleted;
}

int Setup::getPhoneNumberCount()
{
  int count = EEPROM.read(PHONE_NUMBER_COUNT_ADDRESS);
  return count;
}

void Setup::setPhoneNumberCount(int count)
{
  EEPROM.write(PHONE_NUMBER_COUNT_ADDRESS, count);
}


void Setup::getPhoneNumberAtIndex(int index, char* buffer)
{
  int address = PHONE_NUMBERS_START_ADDRESS + PHONE_NUMBER_BYTES * index;  

  int readBytes = m_myEeprom.readBlock(address, (byte*)buffer, PHONE_NUMBER_BYTES); 
  buffer[PHONE_NUMBER_BYTES] = '\0'; //Terminamos la cadena

  //TODO: comprobar que es un número válido de móvil, y sino, devolver null

  /*
  Serial << F("Leido el numero de teléfono ");
   Serial.println(buffer);
   Serial << F("De la posición de memoria ");
   Serial.println(address);
   Serial << F("Bytes leídos ");
   Serial.println(readBytes);
   */
}

void Setup::setPhoneNumber(const char* phoneNumber, int index)
{
  int address = PHONE_NUMBERS_START_ADDRESS + PHONE_NUMBER_BYTES * index;
  int writtenBytes = m_myEeprom.writeBlock(address, (byte*)phoneNumber, PHONE_NUMBER_BYTES);
  /*
  Serial << F("Escrito el numero de teléfono ");
   Serial.println(phoneNumber);
   Serial << F("En la posición de memoria ");
   Serial.println(address);
   
   Serial << F("Bytes escritos ");
   Serial.println(writtenBytes);
   */
}




